package des

import (
	"crypto/des"
	"fmt"
	"gitee.com/gcom/gbox/crypto"
)

type desCrypt struct {
	crypto.CipherCrypt
}

//NewDESCrypt news a DESCrypt pointer with key
func NewDESCrypt(key []byte) *desCrypt {
	block, err := des.NewCipher(key)
	if err != nil {
		panic(err)
	}
	return &desCrypt{CipherCrypt: crypto.CipherCrypt{Block: block}}
}

//NewDESCrypt decodes key and then news a DESCrypt pointer with the key decoded
func NewDesCryptWithDecode(key string, keyDataType crypto.Encode) *desCrypt {
	data, err := crypto.DecodeString(key, keyDataType)
	if err != nil {
		panic(fmt.Sprintf("gocrypt decode key error : %v ", err))
	}
	return NewDESCrypt(data)
}

type tripleDESCrypt struct {
	crypto.CipherCrypt
}

//NewTripleDESCrypt news a TripleDESCrypt pointer with key
func NewTripleDESCrypt(key []byte) *tripleDESCrypt {
	if len(key) != 24 {
		panic("triple des key length must be 24")
	}
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		panic(err)
	}
	return &tripleDESCrypt{CipherCrypt: crypto.CipherCrypt{Block: block}}
}

//NewTripleDesCryptWithDecode decodes key and then news a TripleDESCrypt pointer with the key decoded
func NewTripleDesCryptWithDecode(key string, keyDataType crypto.Encode) *tripleDESCrypt {
	data, err := crypto.DecodeString(key, keyDataType)
	if err != nil {
		panic(fmt.Sprintf("gocrypt decode key error : %v ", err))
	}
	return NewTripleDESCrypt(data)
}
